package com.idnx.mynewsdemo.presenter;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;


import com.idnx.mynewsdemo.database.DBHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileOperations {

    private Context context;
    private DBHelper dbHelper;
    private File appDirectory;

    public FileOperations(Context context) {
        this.context = context;
        this.dbHelper = new DBHelper(context);
        createAppDirectory();
    }

    public File getVideoDirectory() {
        File videoDir = context.getExternalFilesDir(Environment.DIRECTORY_MOVIES);
        if (!videoDir.exists()) {
            videoDir.mkdirs();
        }
        return videoDir;
    }

    public List<String> getVideoPaths() {
        File videoDir = getVideoDirectory();
        File[] files = videoDir.listFiles();
        List<String> paths = new ArrayList<>();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".mp4")) { // 假设视频文件以.mp4结尾
                    paths.add(file.getAbsolutePath());
                }
            }
        }
        return paths;
    }

    private void createAppDirectory() {
        // 创建应用专属目录
        appDirectory = new File(context.getExternalFilesDir(null), "MyVideoApp");
        if (!appDirectory.exists()) {
            appDirectory.mkdirs();
        }
    }


    private void copyFileFromRaw(String fileName, File destFile) throws IOException {
        InputStream inputStream = context.getResources().openRawResource(context.getResources().getIdentifier(fileName, "raw", context.getPackageName()));
        try {
            OutputStream outputStream = new FileOutputStream(destFile);
            try {
                byte[] buffer = new byte[1024];
                int length;
                while ((length = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, length);
                }
            } finally {
                outputStream.close();
            }
        } finally {
            inputStream.close();
        }
    }

    public void copyFilesToAppDirectory() {
        try {
            // 复制视频文件到应用目录
            copyFileFromRaw("game", new File(appDirectory, "game.mp4"));
            copyFileFromRaw("study", new File(appDirectory, "study.mp4"));

            // 复制头像文件到应用目录
            copyFileFromRaw("sytx", new File(appDirectory, "sytx.jpg"));
            copyFileFromRaw("sytx2", new File(appDirectory, "sytx2.jpg"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void scanAndUploadFilesToDatabase() {
        List<File> filesToUpload = scanDirectory(appDirectory);
        for (File file : filesToUpload) {
            if (file.getName().endsWith(".mp4")) {
                // 将视频文件信息添加到数据库
                addVideoToDatabase("Video Title", "Video Description", file.getAbsolutePath());
            } else if (file.getName().endsWith(".jpg")) {
                // 将头像文件信息添加到数据库
                addUserToDatabase("Username", "Password", file.getAbsolutePath());
            }
        }
    }

    private List<File> scanDirectory(File directory) {
        File[] files = directory.listFiles();
        List<File> fileList = new ArrayList<>();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    fileList.add(file);
                }
            }
        }
        return fileList;
    }

    private void addVideoToDatabase(String title, String description, String videoPath) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.getTitleColumn(), title);
        values.put(DBHelper.getDescriptionColumn(), description);
        values.put(DBHelper.getVideoPathColumn(), videoPath);
        db.insert(DBHelper.getVideosTable(), null, values);
    }

    private void addUserToDatabase(String username, String password, String avatarPath) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.getUsernameColumn(), username);
        values.put(DBHelper.getPasswordColumn(), password);
        values.put(DBHelper.getAvatarPathColumn(), avatarPath);
        db.insert(DBHelper.getUsersTable(), null, values);
    }
}